package br.unesp.rc.locadora.connection.hibernate;

import br.unesp.rc.locadora.beans.*;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.CachedRowSet;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FabricaConexao implements Conexao {

    private FabricaConexao() {
        
    }

    private static SessionFactory sf;
    
    public static void criaFabricaSessao(){
            if(sf == null){
                Configuration con = new Configuration();

                con.setProperty("hibernate.connection.driver_class", DRIVER);
                con.setProperty("hibernate.connection.url", URL);
                con.setProperty("hibernate.connection.username", USER);
                con.setProperty("hibernate.connection.password", PASSWORD);
                con.setProperty("hibernate.connection.pool_size", POOL_SIZE);
                con.setProperty("hibernate.dialect", DIALECT);

                con.addAnnotatedClass(Pessoa.class);
                con.addAnnotatedClass(Fisica.class);
                con.addAnnotatedClass(Acesso.class);
                con.addAnnotatedClass(Contato.class);
                con.addAnnotatedClass(Endereco.class);
                con.addAnnotatedClass(Carro.class);
                con.addAnnotatedClass(Categoria.class);
                con.addAnnotatedClass(Fatura.class);
                con.addAnnotatedClass(Fisica.class);
                con.addAnnotatedClass(ItemFatura.class);
                con.addAnnotatedClass(Juridica.class);
                con.addAnnotatedClass(Locacao.class);
                con.addAnnotatedClass(Servico.class);
                con.addResource("br/unesp/rc/locadora/connection/hibernate/procedures.hbm.xml");

                sf = con.buildSessionFactory();
            }
    }
    
    public static Session getSessao() {
        Session session = null;

        try {
            criaFabricaSessao();
            
            session = sf.openSession();
            
        } catch (HibernateException ex) {
            System.out.println("Problemas ao carregar a Sessão!");
            System.out.println("ERRO: " + ex.getMessage());
        }

        return session;
    }
    
    public static Session getSessaoProc() {
        Session session = null;

        try {
            criaFabricaSessao();
            
            session = sf.openSession();
            
        } catch (HibernateException ex) {
            System.out.println("Problemas ao carregar a Sessão!");
            System.out.println("ERRO: " + ex.getMessage());
        }

        return session;
    }

    public static Session getSessaoCache() {
        Session session = null;

        try {
            Configuration con = new Configuration();
            
            con.setProperty("hibernate.connection.driver_class", DRIVER);
            con.setProperty("hibernate.connection.url", URL);
            con.setProperty("hibernate.connection.username", USER);
            con.setProperty("hibernate.connection.password", PASSWORD);
            con.setProperty("hibernate.connection.pool_size", POOL_SIZE);
            con.setProperty("hibernate.dialect", DIALECT);
            
            con.addAnnotatedClass(Pessoa.class);
            con.addAnnotatedClass(Fisica.class);
            con.addAnnotatedClass(Acesso.class);
            con.addAnnotatedClass(Contato.class);
            con.addAnnotatedClass(Endereco.class);
            
            SessionFactory sf = con.buildSessionFactory();
            
            session = sf.openSession();
            
        } catch (HibernateException ex) {
            System.out.println("Problemas ao carregar a Sessão!");
            System.out.println("ERRO: " + ex.getMessage());
        }

        return session;
    }
    
    public static void fechar(Session session) {
        try { 
           session.disconnect();
           session.close();
        } catch (HibernateException ex) {
            System.out.println("Problemas ao fechar: " + ex.getMessage());
        }
    }
}
